/**
 * Created by jiangzhiyong on 15/10/14.
 */

var Index = {
    init: function() {
        Index.init_iframe();
        Index.init_tab();
        $("#logout").click(function(){
        		console.log("123");
        		ajax("/logout",{});
        		$.cookie("token","", { expires: Number.MAX_VALUE, path: '/' });
        		window.location.href = "login.html";
        });
        $("#username").text($.cookie("name"));
        $("#update_password").click(function(){
        		$("#update_password_div").find("[name=old_password]").val("");
        		$("#update_password_div").find("[name=new_password]").val("");
        		$("#update_password_div").find("[name=new_password2]").val("");
        		$("#update_password_div").modal("show");
        });
        $("[name=btn_update_password]").click(function(){
        		var old_password = $("#update_password_div").find("[name=old_password]").val();
        		var new_password = $("#update_password_div").find("[name=new_password]").val();
        		var new_password2 = $("#update_password_div").find("[name=new_password2]").val();
        		if (old_password == "") {
        			alert("请输入原密码");
        			return false;
        		}
        		if (new_password == "") {
        			alert("请输入新密码");
        			return false;
        		}
        		if (new_password2 == "") {
        			alert("请重复输入新密码");
        			return false;
        		}
        		if (new_password != new_password2) {
        			alert("两次输入的新密码不一致");
        			return false;
        		}
        		var data = {
        			"old_password" : calcMD5(old_password + "meetclass" + old_password),
        			"password" : calcMD5(new_password + "meetclass" + new_password)
        		};
        		ajax("/password/update", data, callback_password);
        });
        $("#reset_password").click(function(){
        		$("#reset_password_div").find("[name=mobile]").val("");
        		$("#reset_password_div").find("[name=vcode]").val("");
        		$("#reset_password_div").find("[name=new_password]").val("");
        		$("#reset_password_div").find("[name=new_password2]").val("");
        		$("#reset_password_div").modal("show");
        });
        $("#reset_password_div").find("[name=send_vcode]").click(function(){
        		var mobile = $("#reset_password_div").find("[name=mobile]").val();
        		if (mobile == "") {
        			alert("请输入手机号");
        			return false;
        		}
        		var data = {
        			"mobile" : mobile
        		};
        		ajax("/sms-code/send", data, callback_vcode);
        });
        $("#reset_password_div").find("[name=vcode]").blur(function(){
        		var mobile = $("#reset_password_div").find("[name=mobile]").val();
        		var vcode = $("#reset_password_div").find("[name=vcode]").val();
        		if (mobile == "") {
        			return false;
        		}
        		if (vcode == "") {
        			return false;
        		}
        		var data = {
        			"mobile" : mobile,
        			"vcode" : vcode
        		};
        		ajax("/sms-code/verify", data,callback_checkvcode)
        	});
        $("[name=btn_reset_password]").click(function(){
        		var mobile = $("#reset_password_div").find("[name=mobile]").val();
        		var vcode = $("#reset_password_div").find("[name=vcode]").val();
        		var new_password = $("#reset_password_div").find("[name=new_password]").val();
        		var new_password2 = $("#reset_password_div").find("[name=new_password2]").val();
        		if (mobile == "") {
        			alert("请输入手机号");
        			return false;
        		}
        		if (vcode == "") {
        			alert("请输入验证码");
        			return false;
        		}
        		if (new_password == "") {
        			alert("请输入新密码");
        			return false;
        		}
        		if (new_password2 == "") {
        			alert("请重复输入新密码");
        			return false;
        		}
        		if (new_password != new_password2) {
        			alert("两次输入的新密码不一致");
        			return false;
        		}
        		var data = {
        			"mobile" : mobile,
        			"vcode" : vcode,
        			"password" : calcMD5(new_password + "meetclass" + new_password)
        		};
        		ajax("/password/reset", data, callback_password);
        });
    },
    init_iframe : function() {
        $("#content").load(function(){
            var ifm= document.getElementById("content");
            var nav_left = document.getElementsByClassName("nav_left")[0];
			var subWeb = document.frames ? document.frames["content"].document : ifm.contentDocument;   
			var leftheight = $(window).height()-111;
			if(ifm != null && subWeb != null) {
				var height = subWeb.body.scrollHeight;
				if (height < leftheight) {
					ifm.height = leftheight;
					/*nav_left.height = leftheight;
					$(nav_left).css("height",leftheight); */
				} else{
			   		ifm.height = leftheight;
			   		/*$(nav_left).css("height",height); */
			  	}
			}
			/*弹出框居中  */
//			var diolag = $("#content").contents().find(".diolag");
//			var diolagH = diolag.height();
//			var windowH = $(window).height();
//			var mt = (windowH-diolagH)/2
//			$(diolag).css({'margin-top':-diolagH/2})
			
        });
        var leftheight = $(window).height()-111;
        $(".nav_left").height(leftheight);
    },
//  init_iframe : function() {
//      $("#content").load(function(){
//          var ifm= document.getElementById("content");
//          var nav_left = document.getElementsByClassName("nav_left")[0];
//			var subWeb = document.frames ? document.frames["content"].document : ifm.contentDocument;   
//			var leftheight = $(window).height()-111;
//			if(ifm != null && subWeb != null) {
//				var height = subWeb.body.scrollHeight;
//				if (height < leftheight) {
//					ifm.height = leftheight;
//					/*nav_left.height = leftheight;
//					$(nav_left).css("height",leftheight); */
//				} else{
//			   		ifm.height = leftheight;
//			   		/*$(nav_left).css("height",height); */
//			  	}
//			}
//			/*弹出框居中  */
//			var diolag = $("#content").contents().find(".diolag");
//			var diolagH = diolag.height();
//			var windowH = $(window).height();
//			var mt = (windowH-diolagH)/2
//			$(diolag).css({'margin-top':-diolagH/2})
//			
//      });
//      var leftheight = $(window).height()-111;
//      $(".nav_left").height(leftheight);
//  },
    init_tab : function() {
        $(".nav_ul li").click(function(){
            var This = $(this);
            $(".nav_ul li").css("background-color","");
            $(this).css("background-color","#0879d5").siblings("li").each(function() {
                if (This != $(this)) {
                    $(this).next("ul").slideUp("solw");
                }
            });
            $(this).css("background-color","#0879d5").next("ul").slideDown("solw");
        });
    }
};
function callback_password(result){
	if (result.code == 0) {
		//写入token、写入session
    		var token = result.data.token;
    		var session_id = result.data.sid;
    		var name = result.data.user.name;
    		$.cookie('token', token, { expires: Number.MAX_VALUE, path: '/' });
    		$.cookie('session_id', session_id, { expires: Number.MAX_VALUE, path: '/' });
    		$.cookie('name', name, { expires: Number.MAX_VALUE, path: '/' });
		alert("操作成功");
		$("#update_password_div").modal("hide");
		$("#reset_password_div").modal("hide");
	} else {
		alert(result.msg);
	}
}

function callback_vcode(result) {
	if (result.code == 0) {
		alert("发送成功");
	} else {
		alert(result.msg);
	}
}

function callback_checkvcode(result) {
	if (result.code == 0) {
		$("#reset_password_div").find("[name=check_vcode]").text("验证码正确");
	} else {
		$("#reset_password_div").find("[name=check_vcode]").text(result.msg);
	}
}

$(function(){
	 Index.init();
})
